﻿
namespace Data.Repository
{
	using System;
	using System.Linq;
	using System.Collections.Generic;

	public class OrderRepository : IOrderRepository
	{
		private LinqToECommerceDBDataContext _dbContext;

		#region IOrderRepository Members

		public int CreateNewOrder(Data.Entity.Order theNewOrder)
		{
			if(theNewOrder == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_Order_Create(theNewOrder.UserID, theNewOrder.UserLanguageID, theNewOrder.UserTaxDisplayTypeID,
					theNewOrder.UserIP, theNewOrder.OrderSubtotalInclTax, theNewOrder.OrderSubtotalExclTax, theNewOrder.OrderShippingInclTax, theNewOrder.OrderSubtotalExclTax,
					theNewOrder.PaymentMethodAdditionalFeeInclTax, theNewOrder.PaymentMethodAdditionalFeeExclTax, theNewOrder.OrderTax, theNewOrder.OrderTotal,
					theNewOrder.OrderDiscount, theNewOrder.OrderSubtotalInclTaxInUserCurrency, theNewOrder.OrderSubtotalExclTaxInUserCurrency, theNewOrder.OrderShippingInclTaxInUserCurrency,
					theNewOrder.OrderShippingExclTaxInUserCurrency, theNewOrder.PaymentMethodAdditionalFeeInclTaxInUserCurrency, theNewOrder.PaymentMethodAdditionalFeeExclTaxInUserCurrency,
					theNewOrder.OrderTaxInUserCurrency, theNewOrder.OrderTotalInUserCurrency, theNewOrder.OrderDiscountInUserCurrency, theNewOrder.UserCurrencyCode, theNewOrder.CheckoutAttributeDescription,
					theNewOrder.CheckoutAttributesXML, theNewOrder.OrderWeight, theNewOrder.CashTicketId, theNewOrder.OrderStatusID, theNewOrder.AllowStoringCreditCardNumber,
					theNewOrder.CardType, theNewOrder.CardName, theNewOrder.CardNumber, theNewOrder.MaskedCreditCardNumber, theNewOrder.CardCVV2, theNewOrder.CardExpirationMonth, theNewOrder.CardExpirationYear,
					theNewOrder.PaymentMethodID, theNewOrder.PaymentMethodName, theNewOrder.AuthorizationTransactionID, theNewOrder.AuthorizationTransactionCode, theNewOrder.AuthorizationTransactionResult,
					theNewOrder.CaptureTransactionID, theNewOrder.CaptureTransactionResult, theNewOrder.SubscriptionTransactionID, theNewOrder.PurchaseOrderNumber, theNewOrder.PaymentStatusID,
					theNewOrder.PaidDate, theNewOrder.ShippingStatusID, theNewOrder.ShippingFirstName,
					theNewOrder.ShippingLastName, theNewOrder.ShippingPhoneNumber, theNewOrder.ShippingEmail, theNewOrder.ShippingFaxNumber, theNewOrder.ShippingCompany, theNewOrder.ShippingAddress1,
					theNewOrder.ShippingAddress2, theNewOrder.ShippingCity, theNewOrder.ShippingStateProvince, theNewOrder.ShippingStateProvinceID,
					theNewOrder.ShippingZipPostalCode, theNewOrder.ShippingCountry, theNewOrder.ShippingCountryID, theNewOrder.ShippingMethodID, theNewOrder.ShippingMethod, theNewOrder.ShippedDate, theNewOrder.ShippingMobileNumber,
					theNewOrder.TrackingNumber, theNewOrder.DeliveryDate, theNewOrder.Deleted, theNewOrder.CreatedOn, theNewOrder.ShippingCityID, theNewOrder.Remark, theNewOrder.InvoicingHeader, theNewOrder.InvoicingContent, theNewOrder.DiscountLimit);
			}

			return result;
		}

		public int CreateNewOrderNote(Data.Entity.OrderNote theNewOrderNote)
		{
			if(theNewOrderNote == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_OrderNote_Create(theNewOrderNote.OrderID, theNewOrderNote.Note, theNewOrderNote.DisplayToUser, theNewOrderNote.CreatedOn);
			}

			return result;
		}

		public IList<Data.Entity.OrderNote> GetOrderNoteByOrder(int orderID)
		{
			if(orderID <= 0)
			{
				return null;
			}

			List<ECommerce_GetOrderNote_By_OrderResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetOrderNote_By_Order(orderID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.OrderNote>();

				foreach(var o in lstData)
				{
					var theEntity = new Data.Entity.OrderNote()
					{
						CreatedOn = o.CreatedOn,
						DisplayToUser = o.DisplayToUser,
						Note = o.Note,
						OrderID = o.OrderID,
						OrderNoteID = o.OrderNoteID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public int DeleteOrder(int id)
		{
			if(id <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{ 
				result = _dbContext.ECommerce_Order_Delete(id); 
			}

			return result;
		}

		public IList<Data.Entity.Order> GetOrderByUser(Guid userID)
		{
			if(userID == null)
			{
				return null;
			}

			List<ECommerce_GetOrder_By_UserResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetOrder_By_User(userID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Order>();

				foreach(var o in lstData)
				{
					var theEntity = new Data.Entity.Order()
					{
						CashTicketId = o.CashTicketId,
						AllowStoringCreditCardNumber = o.AllowStoringCreditCardNumber,
						AuthorizationTransactionCode = o.AuthorizationTransactionCode,
						AuthorizationTransactionID = o.AuthorizationTransactionID,
						AuthorizationTransactionResult = o.AuthorizationTransactionResult,
						CaptureTransactionID = o.CaptureTransactionID,
						CaptureTransactionResult = o.CaptureTransactionResult,
						CardCVV2 = o.CardCVV2,
						CardExpirationMonth = o.CardExpirationMonth,
						CardExpirationYear = o.CardExpirationYear,
						CardName = o.CardName,
						CardNumber = o.CardNumber,
						CardType = o.CardType,
						CheckoutAttributeDescription = o.CheckoutAttributeDescription,
						CheckoutAttributesXML = o.CheckoutAttributesXML,
						CreatedOn = o.CreatedOn,
						Deleted = o.Deleted,
						DeliveryDate = o.DeliveryDate,
						MaskedCreditCardNumber = o.MaskedCreditCardNumber,
						OrderDiscount = o.OrderDiscount,
						OrderDiscountInUserCurrency = o.OrderDiscountInUserCurrency,
						OrderNumber = o.OrderNumber,
						OrderID = o.OrderID,
						OrderShippingExclTax = o.OrderShippingExclTax,
						OrderShippingExclTaxInUserCurrency = o.OrderShippingExclTaxInUserCurrency,
						OrderShippingInclTax = o.OrderShippingInclTax,
						OrderShippingInclTaxInUserCurrency = o.OrderShippingInclTaxInUserCurrency,
						OrderStatusID = o.OrderStatusID.GetValueOrDefault(),
						OrderSubtotalExclTax = o.OrderSubtotalExclTax,
						OrderSubtotalExclTaxInUserCurrency = o.OrderSubtotalExclTaxInUserCurrency,
						OrderSubtotalInclTax = o.OrderSubtotalInclTax,
						OrderSubtotalInclTaxInUserCurrency = o.OrderSubtotalInclTaxInUserCurrency,
						OrderTax = o.OrderTax,
						OrderTaxInUserCurrency = o.OrderTaxInUserCurrency,
						OrderTotal = o.OrderTotal,
						OrderTotalInUserCurrency = o.OrderTotalInUserCurrency,
						OrderWeight = o.OrderWeight,
						PaidDate = o.PaidDate,
						PaymentMethodAdditionalFeeExclTax = o.PaymentMethodAdditionalFeeExclTax,
						PaymentMethodAdditionalFeeExclTaxInUserCurrency = o.PaymentMethodAdditionalFeeExclTaxInUserCurrency,
						PaymentMethodAdditionalFeeInclTax = o.PaymentMethodAdditionalFeeInclTax,
						PaymentMethodAdditionalFeeInclTaxInUserCurrency = o.PaymentMethodAdditionalFeeInclTaxInUserCurrency,
						PaymentMethodID = o.PaymentMethodID,
						PaymentMethodName = o.PaymentMethodName,
						PaymentStatusID = o.PaymentStatusID,
						PurchaseOrderNumber = o.PurchaseOrderNumber,
						ShippedDate = o.ShippedDate,
						ShippingAddress1 = o.ShippingAddress1,
						ShippingAddress2 = o.ShippingAddress2,
						ShippingCity = o.ShippingCity,
						ShippingCompany = o.ShippingCompany,
						ShippingCountry = o.ShippingCountry,
						ShippingCountryID = o.ShippingCountryID,
						ShippingEmail = o.ShippingEmail,
						ShippingFaxNumber = o.ShippingFaxNumber,
						ShippingFirstName = o.ShippingFirstName,
						ShippingLastName = o.ShippingLastName,
						ShippingMethodID = o.ShippingMethodID,
						ShippingMethod = o.ShippingMethod,
						ShippingPhoneNumber = o.ShippingPhoneNumber,
						ShippingRateComputationMethodID = o.ShippingRateComputationMethodID,
						ShippingStateProvince = o.ShippingStateProvince,
						ShippingStateProvinceID = o.ShippingStateProvinceID,
						ShippingStatusID = o.ShippingStatusID,
						ShippingZipPostalCode = o.ShippingZipPostalCode,
						ShippingMobileNumber = o.ShippingMobileNumber,
						SubscriptionTransactionID = o.SubscriptionTransactionID,
						TrackingNumber = o.TrackingNumber,
						UserCurrencyCode = o.UserCurrencyCode,
						UserID = o.UserID,
						UserIP = o.UserIP,
						UserLanguageID = o.UserLanguageID,
						UserTaxDisplayTypeID = o.UserTaxDisplayTypeID,
						ShippingCityID = o.ShippingCityID,
						InvoicingContent = o.InvoicingContent,
						InvoicingHeader = o.InvoicingHeader,
						Remark = o.Remark,
						DiscountLimit = o.DiscountLimit
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public IList<Data.Entity.OrderStatus> GetAllOrderStatus()
		{
			List<ECommerce_GetOrderStatusResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetOrderStatus();

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.OrderStatus>();

				foreach(var os in lstData)
				{
					var theEntity = new Data.Entity.OrderStatus()
					{
						Name = os.Name,
						OrderStatusID = os.OrderStatusID,
						OrderStatusNumber = os.OrderStatusNumber
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public int UpdateOrder(Data.Entity.Order theOrder)
		{
			if(theOrder == null || theOrder.OrderID <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbOrder = (from o in _dbContext.Orders
							   where o.OrderID == theOrder.OrderID
							   select o).FirstOrDefault();

				if(dbOrder == null)
				{
					result = 0;
				}
				else
				{
					dbOrder.CashTicketId = theOrder.CashTicketId;
					dbOrder.AllowStoringCreditCardNumber = theOrder.AllowStoringCreditCardNumber;
					dbOrder.AuthorizationTransactionCode = theOrder.AuthorizationTransactionCode;
					dbOrder.AuthorizationTransactionID = theOrder.AuthorizationTransactionID;
					dbOrder.AuthorizationTransactionResult = theOrder.AuthorizationTransactionResult;
					dbOrder.CaptureTransactionID = theOrder.CaptureTransactionID;
					dbOrder.CaptureTransactionResult = theOrder.CaptureTransactionResult;
					dbOrder.CardCVV2 = theOrder.CardCVV2;
					dbOrder.CardExpirationMonth = theOrder.CardExpirationMonth;
					dbOrder.CardExpirationYear = theOrder.CardExpirationYear;
					dbOrder.CardName = theOrder.CardName;
					dbOrder.CardNumber = theOrder.CardNumber;
					dbOrder.CardType = theOrder.CardType;
					dbOrder.CheckoutAttributeDescription = theOrder.CheckoutAttributeDescription;
					dbOrder.CheckoutAttributesXML = theOrder.CheckoutAttributesXML;
					dbOrder.CreatedOn = theOrder.CreatedOn;
					dbOrder.Deleted = theOrder.Deleted;
					dbOrder.DeliveryDate = theOrder.DeliveryDate;
					dbOrder.MaskedCreditCardNumber = theOrder.MaskedCreditCardNumber;
					dbOrder.OrderDiscount = theOrder.OrderDiscount;
					dbOrder.OrderDiscountInUserCurrency = theOrder.OrderDiscountInUserCurrency;
					dbOrder.OrderNumber = theOrder.OrderNumber;
					dbOrder.OrderID = theOrder.OrderID;
					dbOrder.OrderShippingExclTax = theOrder.OrderShippingExclTax;
					dbOrder.OrderShippingExclTaxInUserCurrency = theOrder.OrderShippingExclTaxInUserCurrency;
					dbOrder.OrderShippingInclTax = theOrder.OrderShippingInclTax;
					dbOrder.OrderShippingInclTaxInUserCurrency = theOrder.OrderShippingInclTaxInUserCurrency;
					dbOrder.OrderStatusID = theOrder.OrderStatusID;
					dbOrder.OrderSubtotalExclTax = theOrder.OrderSubtotalExclTax;
					dbOrder.OrderSubtotalExclTaxInUserCurrency = theOrder.OrderSubtotalExclTaxInUserCurrency;
					dbOrder.OrderSubtotalInclTax = theOrder.OrderSubtotalInclTax;
					dbOrder.OrderSubtotalInclTaxInUserCurrency = theOrder.OrderSubtotalInclTaxInUserCurrency;
					dbOrder.OrderTax = theOrder.OrderTax;
					dbOrder.OrderTaxInUserCurrency = theOrder.OrderTaxInUserCurrency;
					dbOrder.OrderTotal = theOrder.OrderTotal;
					dbOrder.OrderTotalInUserCurrency = theOrder.OrderTotalInUserCurrency;
					dbOrder.OrderWeight = theOrder.OrderWeight;
					dbOrder.PaidDate = theOrder.PaidDate;
					dbOrder.PaymentMethodAdditionalFeeExclTax = theOrder.PaymentMethodAdditionalFeeExclTax;
					dbOrder.PaymentMethodAdditionalFeeExclTaxInUserCurrency = theOrder.PaymentMethodAdditionalFeeExclTaxInUserCurrency;
					dbOrder.PaymentMethodAdditionalFeeInclTax = theOrder.PaymentMethodAdditionalFeeInclTax;
					dbOrder.PaymentMethodAdditionalFeeInclTaxInUserCurrency = theOrder.PaymentMethodAdditionalFeeInclTaxInUserCurrency;
					dbOrder.PaymentMethodID = theOrder.PaymentMethodID;
					dbOrder.PaymentMethodName = theOrder.PaymentMethodName;
					dbOrder.PaymentStatusID = theOrder.PaymentStatusID;
					dbOrder.PurchaseOrderNumber = theOrder.PurchaseOrderNumber;
					dbOrder.ShippedDate = theOrder.ShippedDate;
					dbOrder.ShippingAddress1 = theOrder.ShippingAddress1;
					dbOrder.ShippingAddress2 = theOrder.ShippingAddress2;
					dbOrder.ShippingCity = theOrder.ShippingCity;
					dbOrder.ShippingCompany = theOrder.ShippingCompany;
					dbOrder.ShippingCountry = theOrder.ShippingCountry;
					dbOrder.ShippingCountryID = theOrder.ShippingCountryID;
					dbOrder.ShippingEmail = theOrder.ShippingEmail;
					dbOrder.ShippingFaxNumber = theOrder.ShippingFaxNumber;
					dbOrder.ShippingFirstName = theOrder.ShippingFirstName;
					dbOrder.ShippingLastName = theOrder.ShippingLastName;
					dbOrder.ShippingMethod = theOrder.ShippingMethod;
					dbOrder.ShippingPhoneNumber = theOrder.ShippingPhoneNumber;
					dbOrder.ShippingRateComputationMethodID = theOrder.ShippingRateComputationMethodID;
					dbOrder.ShippingStateProvince = theOrder.ShippingStateProvince;
					dbOrder.ShippingStateProvinceID = theOrder.ShippingStateProvinceID;
					dbOrder.ShippingStatusID = theOrder.ShippingStatusID;
					dbOrder.ShippingZipPostalCode = theOrder.ShippingZipPostalCode;
					dbOrder.SubscriptionTransactionID = theOrder.SubscriptionTransactionID;
					dbOrder.TrackingNumber = theOrder.TrackingNumber;
					dbOrder.UserCurrencyCode = theOrder.UserCurrencyCode;
					dbOrder.UserID = theOrder.UserID;
					dbOrder.UserIP = theOrder.UserIP;
					dbOrder.UserLanguageID = theOrder.UserLanguageID;
					dbOrder.UserTaxDisplayTypeID = theOrder.UserTaxDisplayTypeID;
					dbOrder.ShippingMobileNumber = theOrder.ShippingMobileNumber;
					dbOrder.ShippingCityID = theOrder.ShippingCityID;
					dbOrder.Remark = theOrder.Remark;
					dbOrder.InvoicingContent = theOrder.InvoicingContent;
					dbOrder.InvoicingHeader = theOrder.InvoicingHeader;
					dbOrder.ShippingMethodID = theOrder.ShippingMethodID;
					dbOrder.DiscountLimit = theOrder.DiscountLimit;

					_dbContext.SubmitChanges();

					result = 1;
				}
			}

			return result;
		}

		public int DeleteTheOrdersProduct(int orderID, int productID)
		{
			if(orderID <= 0 || productID <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_Order_Product_Mapping_Delete(orderID, productID);
			}

			return result;
		}

		public IList<Data.Entity.Product> GetTheOrdersProducts(int orderID)
		{
			if(orderID <= 0)
			{
				return null;
			}

			List<ECommerce_GetTheOrderProductsResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetTheOrderProducts(orderID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Product>();

				foreach(var p in lstData)
				{
					var theEntity = new Data.Entity.Product()
					{
						AllowUserRatings = p.AllowUserRatings,
						AllowUserReviews = p.AllowUserReviews,
						Cost = p.Cost,
						CreatedOn = p.CreatedOn,
						Deleted = p.Deleted,
						FullDescription = p.FullDescription,
						ListPrice = p.ListPrice,
						MetaKeywords = p.MetaKeywords,
						MetaTitle = p.MetaTitle,
						Name = p.Name,
						ProductID = p.ProductID,
						Published = p.Published,
						Quantity = p.Quantity,
						RatingSum = p.RatingSum,
						ShortDescription = p.ShortDescription,
						ShowOnHomePage = p.ShowOnHomePage,
						Size = p.Size,
						SizeUnit = p.SizeUnit,
						TotalRatingVotes = p.TotalRatingVotes,
						UpdatedOn = p.UpdatedOn,
						Weight = p.Weight,
						WeightUnit = p.WeightUnit,
						Recommend = p.Recommend,
						LanguageID = p.LanguageID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public int CreateNewOrderProductMapping(Data.Entity.Order_Product_Mapping theNewOrder_Product_Mapping)
		{
			if(theNewOrder_Product_Mapping == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{

				result = _dbContext.ECommerce_Order_Product_Mapping_Create(theNewOrder_Product_Mapping.OrderID,
					theNewOrder_Product_Mapping.ProductID, theNewOrder_Product_Mapping.Quantity, theNewOrder_Product_Mapping.CreatedOn, theNewOrder_Product_Mapping.UpdatedOn);
			}

			return result;
		}

		public Data.Entity.Order GetTheOrderByOrderID(int orderID)
		{
			if(orderID <= 0)
			{
				return null;
			}

			List<ECommerce_GetTheOrder_By_OrderIDResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetTheOrder_By_OrderID(orderID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var o = lstData[0];

				Data.Entity.Order result;

				result = new Data.Entity.Order()
				{
					CashTicketId = o.CashTicketId,
					AllowStoringCreditCardNumber = o.AllowStoringCreditCardNumber,
					AuthorizationTransactionCode = o.AuthorizationTransactionCode,
					AuthorizationTransactionID = o.AuthorizationTransactionID,
					AuthorizationTransactionResult = o.AuthorizationTransactionResult,
					CaptureTransactionID = o.CaptureTransactionID,
					CaptureTransactionResult = o.CaptureTransactionResult,
					CardCVV2 = o.CardCVV2,
					CardExpirationMonth = o.CardExpirationMonth,
					CardExpirationYear = o.CardExpirationYear,
					CardName = o.CardName,
					CardNumber = o.CardNumber,
					CardType = o.CardType,
					CheckoutAttributeDescription = o.CheckoutAttributeDescription,
					CheckoutAttributesXML = o.CheckoutAttributesXML,
					CreatedOn = o.CreatedOn,
					Deleted = o.Deleted,
					DeliveryDate = o.DeliveryDate,
					MaskedCreditCardNumber = o.MaskedCreditCardNumber,
					OrderDiscount = o.OrderDiscount,
					OrderDiscountInUserCurrency = o.OrderDiscountInUserCurrency,
					OrderNumber = o.OrderNumber,
					OrderID = o.OrderID,
					OrderShippingExclTax = o.OrderShippingExclTax,
					OrderShippingExclTaxInUserCurrency = o.OrderShippingExclTaxInUserCurrency,
					OrderShippingInclTax = o.OrderShippingInclTax,
					OrderShippingInclTaxInUserCurrency = o.OrderShippingInclTaxInUserCurrency,
					OrderStatusID = o.OrderStatusID.GetValueOrDefault(),
					OrderSubtotalExclTax = o.OrderSubtotalExclTax,
					OrderSubtotalExclTaxInUserCurrency = o.OrderSubtotalExclTaxInUserCurrency,
					OrderSubtotalInclTax = o.OrderSubtotalInclTax,
					OrderSubtotalInclTaxInUserCurrency = o.OrderSubtotalInclTaxInUserCurrency,
					OrderTax = o.OrderTax,
					OrderTaxInUserCurrency = o.OrderTaxInUserCurrency,
					OrderTotal = o.OrderTotal,
					OrderTotalInUserCurrency = o.OrderTotalInUserCurrency,
					OrderWeight = o.OrderWeight,
					PaidDate = o.PaidDate,
					PaymentMethodAdditionalFeeExclTax = o.PaymentMethodAdditionalFeeExclTax,
					PaymentMethodAdditionalFeeExclTaxInUserCurrency = o.PaymentMethodAdditionalFeeExclTaxInUserCurrency,
					PaymentMethodAdditionalFeeInclTax = o.PaymentMethodAdditionalFeeInclTax,
					PaymentMethodAdditionalFeeInclTaxInUserCurrency = o.PaymentMethodAdditionalFeeInclTaxInUserCurrency,
					PaymentMethodID = o.PaymentMethodID,
					PaymentMethodName = o.PaymentMethodName,
					PaymentStatusID = o.PaymentStatusID,
					PurchaseOrderNumber = o.PurchaseOrderNumber,
					ShippedDate = o.ShippedDate,
					ShippingAddress1 = o.ShippingAddress1,
					ShippingAddress2 = o.ShippingAddress2,
					ShippingCity = o.ShippingCity,
					ShippingCompany = o.ShippingCompany,
					ShippingCountry = o.ShippingCountry,
					ShippingCountryID = o.ShippingCountryID,
					ShippingEmail = o.ShippingEmail,
					ShippingFaxNumber = o.ShippingFaxNumber,
					ShippingFirstName = o.ShippingFirstName,
					ShippingLastName = o.ShippingLastName,
					ShippingMethodID = o.ShippingMethodID,
					ShippingMethod = o.ShippingMethod,
					ShippingPhoneNumber = o.ShippingPhoneNumber,
					ShippingRateComputationMethodID = o.ShippingRateComputationMethodID,
					ShippingStateProvince = o.ShippingStateProvince,
					ShippingStateProvinceID = o.ShippingStateProvinceID,
					ShippingStatusID = o.ShippingStatusID,
					ShippingZipPostalCode = o.ShippingZipPostalCode,
					ShippingMobileNumber = o.ShippingMobileNumber,
					SubscriptionTransactionID = o.SubscriptionTransactionID,
					TrackingNumber = o.TrackingNumber,
					UserCurrencyCode = o.UserCurrencyCode,
					UserID = o.UserID,
					UserIP = o.UserIP,
					UserLanguageID = o.UserLanguageID,
					UserTaxDisplayTypeID = o.UserTaxDisplayTypeID,
					ShippingCityID = o.ShippingCityID,
					InvoicingContent = o.InvoicingContent,
					InvoicingHeader = o.InvoicingHeader,
					Remark = o.Remark,
					DiscountLimit = o.DiscountLimit
				};

				return result;
			}

			return null;
		}

		public IList<Data.Entity.Order_Product_Mapping> GetOrderProductMappingByOrder(int theOrderID)
		{
			if(theOrderID <= 0)
			{
				return null;
			}

			List<ECommerce_GetOrder_Product_Mapping_By_OrderIDResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetOrder_Product_Mapping_By_OrderID(theOrderID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Order_Product_Mapping>();

				foreach(var op in lstData)
				{
					var theEntity = new Data.Entity.Order_Product_Mapping()
					{
						CreatedOn = op.CreatedOn,
						UpdatedOn = op.UpdatedOn,
						Quantity = op.Quantity,
						ProductID = op.ProductID,
						OrderID = op.OrderID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public IList<Data.Entity.Order> GetOrdersByLanguage(int languageID)
		{
			if(languageID <= 0)
			{
				return null;
			}

			List<Data.Order> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.Orders.Where(exp => exp.UserLanguageID.Equals(languageID));

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Order>();

				foreach(var o in lstData)
				{
					var theEntity = new Data.Entity.Order()
					{
						CashTicketId = o.CashTicketId,
						AllowStoringCreditCardNumber = o.AllowStoringCreditCardNumber,
						AuthorizationTransactionCode = o.AuthorizationTransactionCode,
						AuthorizationTransactionID = o.AuthorizationTransactionID,
						AuthorizationTransactionResult = o.AuthorizationTransactionResult,
						CaptureTransactionID = o.CaptureTransactionID,
						CaptureTransactionResult = o.CaptureTransactionResult,
						CardCVV2 = o.CardCVV2,
						CardExpirationMonth = o.CardExpirationMonth,
						CardExpirationYear = o.CardExpirationYear,
						CardName = o.CardName,
						CardNumber = o.CardNumber,
						CardType = o.CardType,
						CheckoutAttributeDescription = o.CheckoutAttributeDescription,
						CheckoutAttributesXML = o.CheckoutAttributesXML,
						CreatedOn = o.CreatedOn,
						Deleted = o.Deleted,
						DeliveryDate = o.DeliveryDate,
						MaskedCreditCardNumber = o.MaskedCreditCardNumber,
						OrderDiscount = o.OrderDiscount,
						OrderDiscountInUserCurrency = o.OrderDiscountInUserCurrency,
						OrderNumber = o.OrderNumber,
						OrderID = o.OrderID,
						OrderShippingExclTax = o.OrderShippingExclTax,
						OrderShippingExclTaxInUserCurrency = o.OrderShippingExclTaxInUserCurrency,
						OrderShippingInclTax = o.OrderShippingInclTax,
						OrderShippingInclTaxInUserCurrency = o.OrderShippingInclTaxInUserCurrency,
						OrderStatusID = o.OrderStatusID.GetValueOrDefault(),
						OrderSubtotalExclTax = o.OrderSubtotalExclTax,
						OrderSubtotalExclTaxInUserCurrency = o.OrderSubtotalExclTaxInUserCurrency,
						OrderSubtotalInclTax = o.OrderSubtotalInclTax,
						OrderSubtotalInclTaxInUserCurrency = o.OrderSubtotalInclTaxInUserCurrency,
						OrderTax = o.OrderTax,
						OrderTaxInUserCurrency = o.OrderTaxInUserCurrency,
						OrderTotal = o.OrderTotal,
						OrderTotalInUserCurrency = o.OrderTotalInUserCurrency,
						OrderWeight = o.OrderWeight,
						PaidDate = o.PaidDate,
						PaymentMethodAdditionalFeeExclTax = o.PaymentMethodAdditionalFeeExclTax,
						PaymentMethodAdditionalFeeExclTaxInUserCurrency = o.PaymentMethodAdditionalFeeExclTaxInUserCurrency,
						PaymentMethodAdditionalFeeInclTax = o.PaymentMethodAdditionalFeeInclTax,
						PaymentMethodAdditionalFeeInclTaxInUserCurrency = o.PaymentMethodAdditionalFeeInclTaxInUserCurrency,
						PaymentMethodID = o.PaymentMethodID,
						PaymentMethodName = o.PaymentMethodName,
						PaymentStatusID = o.PaymentStatusID,
						PurchaseOrderNumber = o.PurchaseOrderNumber,
						ShippedDate = o.ShippedDate,
						ShippingAddress1 = o.ShippingAddress1,
						ShippingAddress2 = o.ShippingAddress2,
						ShippingCity = o.ShippingCity,
						ShippingCompany = o.ShippingCompany,
						ShippingCountry = o.ShippingCountry,
						ShippingCountryID = o.ShippingCountryID,
						ShippingEmail = o.ShippingEmail,
						ShippingFaxNumber = o.ShippingFaxNumber,
						ShippingFirstName = o.ShippingFirstName,
						ShippingLastName = o.ShippingLastName,
						ShippingMethodID = o.ShippingMethodID,
						ShippingMethod = o.ShippingMethod,
						ShippingPhoneNumber = o.ShippingPhoneNumber,
						ShippingRateComputationMethodID = o.ShippingRateComputationMethodID,
						ShippingStateProvince = o.ShippingStateProvince,
						ShippingStateProvinceID = o.ShippingStateProvinceID,
						ShippingStatusID = o.ShippingStatusID,
						ShippingZipPostalCode = o.ShippingZipPostalCode,
						ShippingMobileNumber = o.ShippingMobileNumber,
						SubscriptionTransactionID = o.SubscriptionTransactionID,
						TrackingNumber = o.TrackingNumber,
						UserCurrencyCode = o.UserCurrencyCode,
						UserID = o.UserID,
						UserIP = o.UserIP,
						UserLanguageID = o.UserLanguageID,
						UserTaxDisplayTypeID = o.UserTaxDisplayTypeID,
						ShippingCityID = o.ShippingCityID,
						InvoicingContent = o.InvoicingContent,
						InvoicingHeader = o.InvoicingHeader,
						Remark = o.Remark,
						DiscountLimit = o.DiscountLimit
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public Data.Entity.Order GetTheOrderByOrderNumber(string orderNumber)
		{
			if(string.IsNullOrEmpty(orderNumber))
			{
				return null;
			}

			List<ECommerce_GetTheOrder_By_OrderNumberResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetTheOrder_By_OrderNumber(orderNumber);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var o = lstData[0];

				Data.Entity.Order result;

				result = new Data.Entity.Order()
				{
					CashTicketId = o.CashTicketId,
					AllowStoringCreditCardNumber = o.AllowStoringCreditCardNumber,
					AuthorizationTransactionCode = o.AuthorizationTransactionCode,
					AuthorizationTransactionID = o.AuthorizationTransactionID,
					AuthorizationTransactionResult = o.AuthorizationTransactionResult,
					CaptureTransactionID = o.CaptureTransactionID,
					CaptureTransactionResult = o.CaptureTransactionResult,
					CardCVV2 = o.CardCVV2,
					CardExpirationMonth = o.CardExpirationMonth,
					CardExpirationYear = o.CardExpirationYear,
					CardName = o.CardName,
					CardNumber = o.CardNumber,
					CardType = o.CardType,
					CheckoutAttributeDescription = o.CheckoutAttributeDescription,
					CheckoutAttributesXML = o.CheckoutAttributesXML,
					CreatedOn = o.CreatedOn,
					Deleted = o.Deleted,
					DeliveryDate = o.DeliveryDate,
					MaskedCreditCardNumber = o.MaskedCreditCardNumber,
					OrderDiscount = o.OrderDiscount,
					OrderDiscountInUserCurrency = o.OrderDiscountInUserCurrency,
					OrderNumber = o.OrderNumber,
					OrderID = o.OrderID,
					OrderShippingExclTax = o.OrderShippingExclTax,
					OrderShippingExclTaxInUserCurrency = o.OrderShippingExclTaxInUserCurrency,
					OrderShippingInclTax = o.OrderShippingInclTax,
					OrderShippingInclTaxInUserCurrency = o.OrderShippingInclTaxInUserCurrency,
					OrderStatusID = o.OrderStatusID.GetValueOrDefault(),
					OrderSubtotalExclTax = o.OrderSubtotalExclTax,
					OrderSubtotalExclTaxInUserCurrency = o.OrderSubtotalExclTaxInUserCurrency,
					OrderSubtotalInclTax = o.OrderSubtotalInclTax,
					OrderSubtotalInclTaxInUserCurrency = o.OrderSubtotalInclTaxInUserCurrency,
					OrderTax = o.OrderTax,
					OrderTaxInUserCurrency = o.OrderTaxInUserCurrency,
					OrderTotal = o.OrderTotal,
					OrderTotalInUserCurrency = o.OrderTotalInUserCurrency,
					OrderWeight = o.OrderWeight,
					PaidDate = o.PaidDate,
					PaymentMethodAdditionalFeeExclTax = o.PaymentMethodAdditionalFeeExclTax,
					PaymentMethodAdditionalFeeExclTaxInUserCurrency = o.PaymentMethodAdditionalFeeExclTaxInUserCurrency,
					PaymentMethodAdditionalFeeInclTax = o.PaymentMethodAdditionalFeeInclTax,
					PaymentMethodAdditionalFeeInclTaxInUserCurrency = o.PaymentMethodAdditionalFeeInclTaxInUserCurrency,
					PaymentMethodID = o.PaymentMethodID,
					PaymentMethodName = o.PaymentMethodName,
					PaymentStatusID = o.PaymentStatusID,
					PurchaseOrderNumber = o.PurchaseOrderNumber,
					ShippedDate = o.ShippedDate,
					ShippingAddress1 = o.ShippingAddress1,
					ShippingAddress2 = o.ShippingAddress2,
					ShippingCity = o.ShippingCity,
					ShippingCompany = o.ShippingCompany,
					ShippingCountry = o.ShippingCountry,
					ShippingCountryID = o.ShippingCountryID,
					ShippingEmail = o.ShippingEmail,
					ShippingFaxNumber = o.ShippingFaxNumber,
					ShippingFirstName = o.ShippingFirstName,
					ShippingLastName = o.ShippingLastName,
					ShippingMethodID = o.ShippingMethodID,
					ShippingMethod = o.ShippingMethod,
					ShippingPhoneNumber = o.ShippingPhoneNumber,
					ShippingRateComputationMethodID = o.ShippingRateComputationMethodID,
					ShippingStateProvince = o.ShippingStateProvince,
					ShippingStateProvinceID = o.ShippingStateProvinceID,
					ShippingStatusID = o.ShippingStatusID,
					ShippingZipPostalCode = o.ShippingZipPostalCode,
					ShippingMobileNumber = o.ShippingMobileNumber,
					SubscriptionTransactionID = o.SubscriptionTransactionID,
					TrackingNumber = o.TrackingNumber,
					UserCurrencyCode = o.UserCurrencyCode,
					UserID = o.UserID,
					UserIP = o.UserIP,
					UserLanguageID = o.UserLanguageID,
					UserTaxDisplayTypeID = o.UserTaxDisplayTypeID,
					ShippingCityID = o.ShippingCityID,
					InvoicingContent = o.InvoicingContent,
					InvoicingHeader = o.InvoicingHeader,
					Remark = o.Remark,
					DiscountLimit = o.DiscountLimit
				};

				return result;
			}

			return null;
		}

		public Data.Entity.OrderStatus GetTheOrderStatusByStatusName(string statusName)
		{
			if(String.IsNullOrEmpty(statusName))
			{
				return null;
			}

			List<ECommerce_GetTheOrderStatus_By_StatusNameResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetTheOrderStatus_By_StatusName(statusName);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var os = lstData[0];

				Data.Entity.OrderStatus result;

				result = new Data.Entity.OrderStatus()
				{
					Name = os.Name,
					OrderStatusID = os.OrderStatusID,
					OrderStatusNumber = os.OrderStatusNumber
				};

				return result;
			}

			return null;
		}

		#endregion
	}
}
